﻿/* 
 * @autor: hectorea
 * @date: 03/08/12
 * @project: RomanNumbers
 * @reference: 
 *  http://es.wikipedia.org/wiki/Numeraci%C3%B3n_romana
 *  http://en.wikipedia.org/wiki/Roman_numerals
 *  http://www.dacostabalboa.es/wp-content/uploads/2008/05/numeros-romanos.png
 *  http://www.blackwasp.co.uk/NumberToRoman.aspx
 *  http://www.onlineconversion.com/roman_numerals_advanced.htm
 *  http://www.ehow.com/how_5060146_convert-number-roman-numerals.html
 *  http://www.ehow.com/how_8502493_convert-roman-characters-numbers.html
 *  http://www.ehow.com/how_7799048_convert-arabic-numbers.html
*/
using System;
using System.Text;

namespace RomanNumbers
{
    internal static class IntExtension
    {
        #region [ Fields ]
        /// <summary>
        /// Int32 Key values 
        /// </summary>
        readonly static int[] values = new int[] { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
        /// <summary>
        /// Roman Number Key values 
        /// </summary>
        readonly static string[] numerals = new string[] { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
        #endregion

        /// <summary>
        /// Converts a int to its equivalent roman number
        /// </summary>
        /// <param name="number">The input must be in the range of 1 - 3999</param>
        /// <returns>The equivalent represention of <para>number</para> in roman number</returns>
        internal static string ConvertToRoman(this int number)
        {
            if (number < 1 || number > 3999)
                throw new ArgumentOutOfRangeException("The input must be in the range of 1 - 3999");

            StringBuilder result = new StringBuilder();
            
            for (int i = 0; i < 13; i++)
            {
                while (number >= values[i])
                {
                    number -= values[i];
                    result.Append(numerals[i]);
                }
            }

            return result.ToString();
        }

        #region [ Pending ]
        /// <summary>
        /// Converts a int to its equivalent roman number
        /// </summary>
        /// <param name="number">The input must be in the range of 1 - 4999</param>
        /// <returns>The equivalent represention of <para>number</para> in roman number</returns>
        //internal static string ConvertToRoman(this int number)
        //{
        //    if (number < 1 || number > 3999)
        //        throw new ArgumentOutOfRangeException("The input must be in the range of 1 - 4999");

        //    int quotient = number;
        //    int remainder = 0;
        //    int result = 0;
        //    int pow = 1;

        //    do
        //    {
        //        remainder = quotient % 10;
        //        quotient /= 10;

        //        result = pow * remainder;
        //        pow *= 10;

        //        //Get the roman value for this iteration



        //    } while (quotient > 0);

        //    return string.Empty; ;
        //}
        //private static string GetRomanNumber(int number)
        //{
        //    switch (number)
        //    {
        //        case 1:
        //            return "I";
        //        case 5:
        //            return "V";
        //        case 10:
        //            return "X";
        //        case 50:
        //            return "L";
        //        case 100:
        //            return "C";
        //        case 500:
        //            return "D";
        //        case 1000:
        //            return "M";
        //        default:
        //            throw new ArgumentException(string.Format("The input {0} is not a valid number"));

        //    }
        //}
        //private static int GetArabicNumber(char digit)
        //{
        //    switch (digit)
        //    {
        //        case 'I':
        //            return 1;
        //        case 'V':
        //            return 5;
        //        case 'X':
        //            return 10;
        //        case 'L':
        //            return 50;
        //        case 'C':
        //            return 100;
        //        case 'D':
        //            return 500;
        //        case 'M':
        //            return 1000;
        //        default:
        //            throw new ArgumentException(string.Format("The input {0} is not a valid roman number"));
        //    }
        //}
        #endregion
    }
}
